﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using DecodeInterface;

namespace VirtualVisionSpace
{
    public class VisionProvider : IVision
    {
        DecodeExplorer decodeExplorer = null;

        System.Timers.Timer timer = new System.Timers.Timer();

        long sumEnvironment;
        long countEnvironment;
        long realEnvironment;
        Stopwatch watch = new Stopwatch();

        public VisionProvider()
        {
            timer.Interval = 100;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.environmentCallback != null && this.visionCallback != null)
            {
                watch.Reset();
                watch.Start();
                Bitmap image = this.environmentCallback(this.visionRegion);

                realEnvironment = watch.ElapsedMilliseconds;
                sumEnvironment += realEnvironment;
                countEnvironment++;

                if (decodeExplorer != null)
                {
                    decodeExplorer.InputImage(image.Clone() as Bitmap);
                    decodeExplorer.ShowImage();
                }

                this.visionCallback(image);
            }
        }

        #region IVision 成员
        EnvironmentCallback environmentCallback;
        public EnvironmentCallback EnvironmentCallback
        {
            set { environmentCallback = value; }
        }
        VisionCallback visionCallback;
        public VisionCallback VisionCallback
        {
            set { visionCallback = value; }
        }

        public Control DecodeShow
        {
            get
            {
                if (decodeExplorer == null) return null;
                else return decodeExplorer.DecodeShow;
            }
            set
            {
                if (decodeExplorer == null && value != null)
                {
                    this.decodeExplorer = new DecodeExplorer(value);
                }
            }
        }

        Rectangle visionRegion = new Rectangle(0, 0, 704, 576);
        public Rectangle VisionRegion
        {
            get
            {
                return visionRegion;
            }
            set
            {
                visionRegion = value;
            }
        }

        public double Interval
        {
            get
            {
                return this.timer.Interval;
            }
            set
            {
                this.timer.Interval = value;
            }
        }

        public void Start()
        {
            this.timer.Start();
        }

        public void Stop()
        {
            this.timer.Stop();
        }

        #endregion


        #region IVision 成员


        public long RealEvnironmentElapsed
        {
            get { return this.realEnvironment; }
        }

        public long AvgEvnironmentElapsed
        {
            get { return this.sumEnvironment / this.countEnvironment; }
        }

        #endregion
    }
}
